package com.ruizhong.centercontroll.controller;

import com.ruizhong.centercontroll.aspect.myAnnotation.checkLogin;
import com.ruizhong.centercontroll.entity.TScheduledTask;
import com.ruizhong.centercontroll.service.TScheduledTaskService;
import com.ruizhong.centercontroll.task.ScheduledTask;
import com.ruizhong.centercontroll.util.Result;
import com.ruizhong.centercontroll.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/scheduledTask/api")
@CrossOrigin
public class TScheduledTaskController {

    @Autowired
    private TScheduledTaskService tScheduledTaskService;

    @Autowired
    private ScheduledTask scheduledTask;

    @PostMapping("/saveNewScheduledTask")
    @checkLogin("saveNewScheduledTask")
    public Result saveNewScheduledTask(@RequestBody TScheduledTask tScheduledTask) {
        tScheduledTaskService.saveNewScheduledTask(tScheduledTask);
        new Thread(new Runnable() {
            @Override
            public void run() {
                scheduledTask.reInit();
            }
        }).start();
        return new Result(ResultCode.success, "保存成功", null);
    }

    @GetMapping("/getAllScheduledTask")
    @checkLogin("getAllScheduledTask")
    public Result getAllScheduledTask() {
        List<TScheduledTask> tScheduledTasks = tScheduledTaskService.getAllScheduledTask();
        return new Result(ResultCode.success, null, tScheduledTasks);
    }

    @PostMapping("/updateScheduleTaskById")
    @checkLogin("updateScheduledTaskById")
    public Result updateScheduleTaskById(@RequestBody TScheduledTask tScheduledTask) {
        tScheduledTaskService.updateScheduledTaskById(tScheduledTask);
        new Thread(new Runnable() {
            @Override
            public void run() {
                scheduledTask.reInit();
            }
        }).start();
        return new Result(ResultCode.success, "保存成功", null);
    }

    @GetMapping("/test")
    public Result test() {

        return new Result(ResultCode.success, null, null);
    }

    @DeleteMapping("/delTaskById/{id}")
    @checkLogin("delTaskById")
    public Result delTaskById(@PathVariable(value = "id") Integer id) {
        tScheduledTaskService.delTaskById(id);
        new Thread(new Runnable() {
            @Override
            public void run() {
                scheduledTask.reInit();
            }
        }).start();
        return new Result(ResultCode.success, "删除成功", null);
    }

    @GetMapping("/sync")
    public Result sync() {
        tScheduledTaskService.sync();
        return new Result(ResultCode.success, null, null);
    }
}
